package tech.aistar.day09;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/3/18 9:05 上午
 */
public class ThreadSafeDemo implements Runnable{
    //线程不安全的...
    public StringBuilder builder = new StringBuilder();

    //线程安全...
    //public StringBuffer builder = new StringBuffer();

    @Override
    public void run() {
        for (int i = 0; i < 1000000; i++) {
            builder.append("0");
        }
    }
}

class TestSafe{
    public static void main(String[] args) {
        ThreadSafeDemo r = new ThreadSafeDemo();
        //创建了俩个线程...
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);

        t1.start();
        t2.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("===="+r.builder.length());
    }
}
